
package com.hlkj.pay.service.order.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.mybatis.core.query.QueryWrapperX;
import com.hlkj.framework.mybatis.core.utils.MyBatisUtils;
import com.hlkj.pay.dto.order.OrderTransferDto;
import com.hlkj.pay.dto.order.TransferOrderQueryDto;
import com.hlkj.pay.infrastructure.mapper.order.OrderTransferMapper;
import com.hlkj.pay.infrastructure.model.order.OrderTransferDO;
import com.hlkj.pay.service.CommonSnFilterService;
import com.hlkj.pay.service.order.ITransferOrderService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2025/02/08 15:27
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class TransferOrderServiceImpl extends CommonSnFilterService implements ITransferOrderService {

    private final OrderTransferMapper orderTransferMapper;

    @Override
    public OrderTransferDO queryPayOrder(String payOrderNo) {
        return orderTransferMapper.selectOne(OrderTransferDO::getPayOrderNo, payOrderNo);
    }

    @Override
    public int updatePayOrderStatus(OrderTransferDO updatePayOrderDO) {
        return orderTransferMapper.updateById(updatePayOrderDO);
    }

    @Override
    public OrderTransferDO queryByMchOrderNo(String sn, String mchOrderNo) {
        return orderTransferMapper.selectOne(OrderTransferDO::getSn, sn, OrderTransferDO::getMchOrderNo, mchOrderNo);
    }

    @Override
    public void addTransferOrder(OrderTransferDO orderTransfer) {
        orderTransferMapper.insert(orderTransfer);
    }

    @Override
    public PageResult<OrderTransferDto> queryPage(TransferOrderQueryDto commonOrderQueryDto) {
        commonOrderQueryDto.setSortingFields("po.id");
        IPage<OrderTransferDto> mpPage = MyBatisUtils.buildPage(commonOrderQueryDto);
        QueryWrapper<OrderTransferDto> queryWrapper = buildWrapper(commonOrderQueryDto);
        orderTransferMapper.selectPayPage(mpPage, queryWrapper);
        return new PageResult(mpPage.getRecords(), mpPage.getTotal(), mpPage.getCurrent(), mpPage.getSize(), mpPage.getPages());
    }
    /**
     * 查询条件封装
     *
     * @param transferOrderQueryDto
     * @return
     */
    QueryWrapper<OrderTransferDto> buildWrapper(TransferOrderQueryDto transferOrderQueryDto) {
        filterSn(transferOrderQueryDto);
        QueryWrapperX<OrderTransferDto> queryWrapper = new QueryWrapperX<>();
        queryWrapper.likeIfPresent( "po.mch_order_no", transferOrderQueryDto.getMchOrderNo());
        queryWrapper.likeIfPresent( "po.pay_order_no", transferOrderQueryDto.getPayOrderNo());
        queryWrapper.likeIfPresent( "po.channel_order_no", transferOrderQueryDto.getChannelOrderNo());
        queryWrapper.eqIfPresent( "po.sn", transferOrderQueryDto.getSn());
        queryWrapper.inIfPresent( "po.sn", transferOrderQueryDto.getSnList());
        queryWrapper.eqIfPresent( "po.app_id", transferOrderQueryDto.getAppId());
        queryWrapper.eqIfPresent("po.state", transferOrderQueryDto.getStatus());
        queryWrapper.eqIfPresent("po.provider_code", transferOrderQueryDto.getProviderCode());
        queryWrapper.eqIfPresent("po.channel_code", transferOrderQueryDto.getChannelCode());
        queryWrapper.eqIfPresent("po.notify_state", transferOrderQueryDto.getNotifyState());
        queryWrapper.likeIfPresent("po.in_user_id", transferOrderQueryDto.getInUserId());
        queryWrapper.likeIfPresent("po.out_user_id", transferOrderQueryDto.getOutUserId());
        queryWrapper.likeIfPresent("in_report.virtual_account_no", transferOrderQueryDto.getInVirtualAccountNo());
        queryWrapper.likeIfPresent("in_report.name", transferOrderQueryDto.getInUserName());
        queryWrapper.likeIfPresent("out_report.virtual_account_no", transferOrderQueryDto.getOutVirtualAccountNo());
        queryWrapper.likeIfPresent("out_report.name", transferOrderQueryDto.getOutUserName());

        queryWrapper.geIfPresent("po.create_time", transferOrderQueryDto.getStartQueryTime());
        queryWrapper.leIfPresent("po.create_time", transferOrderQueryDto.getEndQueryTime());
        queryWrapper.geIfPresent( "po.amount", transferOrderQueryDto.getMinAmount());
        queryWrapper.leIfPresent("po.amount", transferOrderQueryDto.getMaxAmount());
        return queryWrapper;
    }
}
